package com.lanhe.common.utils.wechat;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import com.wechat.pay.java.service.transferbatch.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class WxTiXianUtil {


    /** 商户号 */
    @Value("${lanhe.wechat.mchid}")
    private   String merchantId;
    /** 商户API私钥路径 */
    @Value("${lanhe.wechat.privateKeyPath}")
    private  String privateKeyPath ;
    /** 商户证书序列号 */
    @Value("${lanhe.wechat.merchantSerialNumber}")
    private  String merchantSerialNumber;
    /** 商户APIV3密钥 */
    @Value("${lanhe.wechat.apiV3}")
    private  String apiV3Key;

    private  TransferBatchService service;

    /**
     * 生成商户订单号
     *
     * @return java.lang.String
     * @author zhanggl
     * @date 2024/1/5
     */
    public  String getBatchNo() {
        return "WXTX" + System.currentTimeMillis();
    }


    public  TransferBatchService config(){
        System.err.println("merchantId"+ merchantId);
        System.err.println("privateKeyPath"+ privateKeyPath);
        System.err.println("merchantSerialNumber"+merchantSerialNumber);
        System.err.println("apiV3Key"+apiV3Key);

        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        // 使用 com.wechat.pay.java.core.util
                        // 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();

        // 初始化服务
        return service = new TransferBatchService.Builder().config(config).build();
    }

    public  TransferBatchEntity getTransferBatchByNo() {

        GetTransferBatchByNoRequest request = new GetTransferBatchByNoRequest();
        return service.getTransferBatchByNo(request);
    }
    /** 通过商家批次单号查询批次单 */
    public  TransferBatchEntity getTransferBatchByOutNo() {

        GetTransferBatchByOutNoRequest request = new GetTransferBatchByOutNoRequest();
        return service.getTransferBatchByOutNo(request);
    }
    /** 发起商家转账 */
    public  InitiateBatchTransferResponse initiateBatchTransfer() {
        InitiateBatchTransferRequest request = new InitiateBatchTransferRequest();

        return service.initiateBatchTransfer(request);
    }
    /** 通过微信明细单号查询明细单 */
    public  TransferDetailEntity getTransferDetailByNo() {

        GetTransferDetailByNoRequest request = new GetTransferDetailByNoRequest();
        return service.getTransferDetailByNo(request);
    }
    /** 通过商家明细单号查询明细单 */
    public  TransferDetailEntity getTransferDetailByOutNo() {

        GetTransferDetailByOutNoRequest request = new GetTransferDetailByOutNoRequest();
        return service.getTransferDetailByOutNo(request);
    }
}
